//@author A0096759E

/**
 * Deserializes Google Calendar Response JSON Objects into an array of ScheduleEvents
 */

package googlecal;

import java.lang.reflect.Type;

import storage.ScheduleEvent;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class GoogleCalendarResponseDeserializer implements JsonDeserializer<GoogleAPIResponse> {

	  private static final String KEYWORD_ITEMS = "items";

	  @Override
	  public GoogleAPIResponse deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
	      throws JsonParseException {
		   final JsonObject jsonObject = json.getAsJsonObject();

		    // Delegate the deserialization to the context
		    final ScheduleEvent[] googleCalendarItems = context.deserialize(jsonObject.get(KEYWORD_ITEMS), ScheduleEvent[].class);
	
		    final GoogleAPIResponse googleCalendarResponse = new GoogleAPIResponse();
		    googleCalendarResponse.setGoogleCalendarItem(googleCalendarItems);
		    return googleCalendarResponse;
	    }
}

